package com.zgh.controller;

import com.alibaba.fastjson.JSON;
import com.zgh.domain.*;
import com.zgh.service.BbsService;
import com.zgh.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    /**
     * 获取所有博客
     * @return 博客集合
     */
    @RequestMapping(value = "/getBlog/{order}/{classifyName}",method = RequestMethod.GET)
    public Result getBlog(@PathVariable String order, @PathVariable String classifyName) {
        List<Blog> blogList = blogService.getAllBlogByTime(order,classifyName);
        ArrayList<Map<String,Object>> blogs = new ArrayList<>();
        if (blogList!=null){
            for (Blog blog : blogList) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("classificationList",blog.getClassificationList());
                map1.put("collections",blog.getCollections());
                map1.put("provinceName",blog.getProvinceName());
                map1.put("comments",blog.getComments());
                map1.put("content",blog.getContent());
                map1.put("id",blog.getId());
                map1.put("user",blog.getUser());
                map1.put("releaseTime",blog.getReleaseTime());
                map1.put("thumbs",blog.getThumbs());
                map1.put("title",blog.getTitle());
                ArrayList<Map<String, Object>> databox = new ArrayList<>();
                Map<String, Object> data = new HashMap<>();
                data.put("name","favor");
                data.put("isIconAct",false);
                data.put("nux",blog.getCollections());
                databox.add(data);
                Map<String, Object> data1 = new HashMap<>();
                data1.put("name","comment");
                data1.put("comment",false);
                data1.put("nux",blog.getComments());
                databox.add(data1);
                Map<String, Object> data2 = new HashMap<>();
                data2.put("name","like");
                data2.put("isIconAct",false);
                data2.put("nux",blog.getThumbs());
                databox.add(data2);
                map1.put("datanum",databox);
                blogs.add(map1);
            }
        }
        return new Result(true,"查询成功",blogs);
    }


    /**
     * 分页查询博客
     * @return 博客集合
     */
    @RequestMapping(value = "/getPageBlog",method = RequestMethod.POST)
    public Result getPageBlog(@RequestBody String params) {
        String pagenum = JSON.parseObject(params).getString("pagenum");
        String pagesize = JSON.parseObject(params).getString("pagesize");
        String provinceName = JSON.parseObject(params).getString("region");
        ipageBlog blogList = blogService.getPageBlogByHots(pagenum,pagesize,provinceName);
        return new Result(true,"查询成功",blogList);
    }

    /**
     * 根据省份查询博客（前2条）
     * @param province 省份名称
     * @return result
     */
    @RequestMapping(value = "/getBlogByProvince/{province}",method = RequestMethod.GET)
    public Result getBlogByProvince(@PathVariable String province) {
        List<Blog> blogList = blogService.getAllBlogByProvince(province);
        return new Result(true,"查询成功",blogList);
    }

    /**
     * 根据id查询博客
     * @param id 博客id
     * @return Result
     */
    @RequestMapping(value = "/getBlogById/{id}",method = RequestMethod.GET)
    public Result getBlogById(@PathVariable String id){
        Blog blog = blogService.findBlogById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("blog",blog);
        ArrayList<Map<String, Object>> databox = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        data.put("name","favor");
        data.put("isIconAct",false);
        data.put("nux",blog.getCollections());
        databox.add(data);
        Map<String, Object> data1 = new HashMap<>();
        data1.put("name","comment");
        data1.put("comment",false);
        data1.put("nux",blog.getComments());
        databox.add(data1);
        Map<String, Object> data2 = new HashMap<>();
        data2.put("name","like");
        data2.put("isIconAct",false);
        data2.put("nux",blog.getThumbs());
        databox.add(data2);
        map.put("databox",databox);
        //System.out.println(bbs);
        return new Result(true,"查询成功",map);
    }

    /**
     * 点赞
     * @param blogId 博客id
     * @param thumbCount 点赞数目
     * @return result
     */
    @RequestMapping(value = "/thumb/{blogId}/{thumbCount}",method = RequestMethod.GET)
    public Result thumb(@PathVariable String blogId,@PathVariable String thumbCount){
        blogService.thumb(blogId,thumbCount);
        return new Result(true,"操作成功",null);
    }

    @RequestMapping(value = "/getCollectCountByBlogId/{blogId}",method = RequestMethod.GET)
    public Result getCollectCountByBlogId(@PathVariable String blogId){
        int count = blogService.getCollectCountByBlogId(blogId);
        return new Result(true,"查询成功",count);
    }

    /**
     * 上传博客
     * @param blog
     * @return 失败或成功
     */
    @RequestMapping(value = "/uploadBlog",method = RequestMethod.POST)
    public Result uploadBlog(@RequestBody String blog){
        Blog blog1 = new Blog();
        blog1.setTitle(JSON.parseObject(blog).getString("title"));
        blog1.setContent(JSON.parseObject(blog).getString("content"));
        blog1.setCoverUrl(JSON.parseObject(blog).getString("coverUrl"));
        blog1.setPosition(JSON.parseObject(blog).getString("position"));
        blog1.setProvinceName(JSON.parseObject(blog).getString("province"));
        blog1.setTag(JSON.parseObject(blog).getString("tag"));
        String openid = JSON.parseObject(blog).getString("openid");
        User user1 = new User();
        user1.setOpenid(openid);
        blog1.setUser(user1);
        int pd = blogService.uploadBlog(blog1);
        if (pd==1){
            return new Result(true,"发布成功",null);
        } else {
            return new Result(false,"发布失败",null);
        }
    }

    /**
     * 根据openid查询博客
     * @param
     * @return Result
     */
    @RequestMapping(value = "/getBlogByOpenid",method = RequestMethod.POST)
    public Result getBlogByOpenid(@RequestBody String openid){
        String openid1 = JSON.parseObject(openid).getString("openid");
        List <Blog> blogList = blogService.findBlogByOpenid(openid1);
        //System.out.println(bbs);
        if (blogList!=null){
            return new Result(true,"查询成功",blogList);
        } else {
            return new Result(false,"查询失败",null);
        }
    }

    /**
     * 根据userId查询博客
     * @param userId 用户id
     * @return Result
     */
    @RequestMapping(value = "/getBlogByUserId",method = RequestMethod.POST)
    public Result getBlogByUserId(@RequestBody String userId){
        String userId1 = JSON.parseObject(userId).getString("userId");
        List <Blog> blogList = blogService.findBlogByUserId(userId1);
        //System.out.println(bbs);
        if (blogList!=null){
            return new Result(true,"查询成功",blogList);
        } else {
            return new Result(false,"查询失败",null);
        }
    }

    /**
     * 根据openid查询用户收藏博客
     * @param
     * @return Result
     */
    @RequestMapping(value = "/getCollectListByOpenid",method = RequestMethod.POST)
    public Result getCollectListByOpenid(@RequestBody String openid){
        String openid1 = JSON.parseObject(openid).getString("openid");
        List <Blog> blogList = blogService.getCollectListByOpenid(openid1);
        if (blogList!=null){
            return new Result(true,"查询收藏博客成功",blogList);
        } else {
            return new Result(false,"查询收藏博客失败",null);
        }
    }


    // 分页查询
    @RequestMapping(value = "/getAllBlogInfo",method = RequestMethod.POST)
    public Result getAllBlogInfo(@RequestBody String condition){
        //System.out.println(JSON.parseObject(condition));
        String query = JSON.parseObject(condition).getString("query");
        int pagenum = JSON.parseObject(condition).getInteger("pagenum");
        int pagesize = JSON.parseObject(condition).getInteger("pagesize");
        System.out.println(pagesize);

        ipageBlog ipageBlog = blogService.getAllBlogInfo(pagenum,pagesize,query);
        //System.out.println(ipageUser);
        if (ipageBlog!=null){
            return new Result(true,"获取博客列表成功",ipageBlog);
        }else {
            return new Result(false, "获取博客列表失败", null);
        }
    }


    // 通过id 删除博客信息
    @RequestMapping(value = "/deleteByid",method = RequestMethod.POST)
    public Result deleteByid(@RequestBody String idStr){
        Integer id = JSON.parseObject(idStr).getInteger("id");
        int count = blogService.deleteByid(id);
        if (count!=0){
            return new Result(true,"删除成功",null);
        }else {
            return new Result(false, "删除失败", null);
        }
    }

    // 通过id获取博客信息
    @RequestMapping(value = "/getInfoByid",method = RequestMethod.POST)
    public Result getUserInfoByid(@RequestBody String idStr){
        Integer id = JSON.parseObject(idStr).getInteger("id");
        Blog blog = blogService.getBlogById(id);
        if (blog!=null){
            return new Result(true,"获取成功",blog);
        }else {
            return new Result(false, "获取失败", null);
        }
    }

    // 通过id修改博客信息
    @RequestMapping(value = "/saveInfoByid",method = RequestMethod.POST)
    public Result saveInfoByid(@RequestBody String blog){
        Blog blog1 = new Blog();
        blog1.setTitle(JSON.parseObject(blog).getString("title"));
        blog1.setTag(JSON.parseObject(blog).getString("tag"));
        blog1.setAuthor(JSON.parseObject(blog).getString("author"));
        blog1.setId(JSON.parseObject(blog).getInteger("id"));
        int count = blogService.saveInfoByid(blog1);
        if (count==1){
            return new Result(true,"保存博客信息成功",null);
        }else {
            return new Result(false, "保存博客信息失败", null);
        }
    }

    // 通过点赞量获取博客
    @RequestMapping(value = "/getBlogByThumb",method = RequestMethod.POST)
    public Result getBlogByThumb(){
       List<Blog> blogList = blogService.getBlogByThumb();
        if (blogList!=null){
            return new Result(true,"获取博客信息成功",blogList);
        }else {
            return new Result(false, "保存博客信息失败", null);
        }
    }

    // 通过标签获取博客
    @RequestMapping(value = "/getBlogByTag",method = RequestMethod.POST)
    public Result getBlogByTag(@RequestBody String tagStr){
        String tag = JSON.parseObject(tagStr).getString("tag");
        List<Blog> blogList = blogService.getBlogByTag(tag);
        if (blogList!=null){
            return new Result(true,"获取博客信息成功",blogList);
        }else {
            return new Result(false, "保存博客信息失败", null);
        }
    }

    // 通过点赞量和搜索词获取博客
    @RequestMapping(value = "/getBlogByValue",method = RequestMethod.POST)
    public Result getBlogByValue(@RequestBody String value){
        String value1 = JSON.parseObject(value).getString("value");
        List<Blog> blogList = blogService.getBlogByValue(value1);
        if (blogList!=null){
            return new Result(true,"获取博客信息成功",blogList);
        }else {
            return new Result(false, "保存博客信息失败", null);
        }
    }

    @RequestMapping(value = "/getCountByUserId/{userId}",method = RequestMethod.GET)
    public Result getCountByUserId(@PathVariable String userId){
        int count = blogService.getBlogCountById(userId);
        if (count!=0){
            return new Result(true,"获取博客数量成功",count);
        }else {
            return new Result(false, "保存博客数量失败", null);
        }
    }
}
